/*
 * NodeStatistics.java
 *
 * Created on January 8, 2010, 7:45 PM
 */

package networkvisualize;

/**
 *
 * @author  hrushi
 */
public class NodeStatistics extends javax.swing.JFrame {

    public static int FORM_WIDTH = 400;
    public static int LEFT_MAC_LABEL = 30;
    public static int TOP_MAC_LABEL = 30;
    public static int HEIGHT_MAC_LABEL = 20;
    public static int WIDTH_MAC_LABEL = 350;
    public static int VERT_GAP_IN_LABEL = 10;
        
    public String mac;
    
    /** Creates new form NodeStatistics */
    public NodeStatistics() {
        
        macLabel = new javax.swing.JLabel();
        nameLabel = new javax.swing.JLabel();
        ipLabel = new javax.swing.JLabel();
        distLabel = new javax.swing.JLabel();
        txLabel = new javax.swing.JLabel();
        rxLabel = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        macLabel.setText("jLabel1");
        distLabel.setText("jLabel1");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(macLabel)
                    .addComponent(nameLabel)
                    .addComponent(ipLabel)
                    .addComponent(distLabel)
                    .addComponent(txLabel)
                    .addComponent(rxLabel))
                .addContainerGap(140, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(macLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(nameLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(ipLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(distLabel)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(txLabel)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(rxLabel)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
        //initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Node Statistics");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
    public void add_data() {
        nameLabel.setBounds(LEFT_MAC_LABEL, TOP_MAC_LABEL, WIDTH_MAC_LABEL, HEIGHT_MAC_LABEL);
        macLabel.setBounds(LEFT_MAC_LABEL, nameLabel.getBounds().y + nameLabel.getBounds().height + VERT_GAP_IN_LABEL, WIDTH_MAC_LABEL, HEIGHT_MAC_LABEL);
        ipLabel.setBounds(LEFT_MAC_LABEL, macLabel.getBounds().y + macLabel.getBounds().height + VERT_GAP_IN_LABEL, WIDTH_MAC_LABEL, HEIGHT_MAC_LABEL);
        distLabel.setBounds(LEFT_MAC_LABEL, ipLabel.getBounds().y + ipLabel.getBounds().height + VERT_GAP_IN_LABEL, WIDTH_MAC_LABEL, HEIGHT_MAC_LABEL);
        
        //String[] strArr1;
        FileHandler fRead = new FileHandler(NetVisualize.statPath);
        FileHandler fhRead = new FileHandler(NetVisualize.hostPath);
        String[] strArr1, strSplit1;
        if (fRead.openFile(0)==true) {
            if (fhRead.openFile(0)==true) {
                strArr1 = fhRead.readIntoArray(); 
                fhRead.closeFile();
                for (int i=0; i<strArr1.length; i++) {
                    strSplit1 = strArr1[i].split(" ");
                    if (strSplit1[2].trim().equals(mac.trim())) {
                        ipLabel.setText("IPv4: " + strSplit1[1]);
                        nameLabel.setText("Name: " + strSplit1[0]);
                        break;
                    }
                }
            }
            
            strArr1 = fRead.readIntoArray();
            fRead.closeFile();
            for (int i = (strArr1.length - 1); i > 0 && (strArr1.length - 1 - i) < NetVisualize.FILE_BUF_LENGTH2; i--)  {
                String[] strArr2 = strArr1[i].split(" "); // used to split lines in strArr1
                if (strArr2.length>1) {
                    if (strArr2[1].trim().equals("SENDER")) {
                       // System.out.println(strArr2[1] + strArr2[3] + mac + ";");        
                        if (strArr2[3].trim().equals(mac)) {
                            System.out.println(strArr2[1] + " " + strArr2[3] + " " + mac + " sent " + strArr2[5]);        
                            txLabel.setText("Transmitted Packets: " + strArr2[5] + " in session " + strArr2[8]);

                            txLabel.setBounds(LEFT_MAC_LABEL, distLabel.getBounds().y + distLabel.getBounds().height + VERT_GAP_IN_LABEL, WIDTH_MAC_LABEL, HEIGHT_MAC_LABEL);
                            //txLabel.setSize(50, 20);
                            System.out.println(macLabel.getBounds().y + "+" + distLabel.getBounds().y + "+" + txLabel.getBounds().y);
                            this.setSize(FORM_WIDTH, txLabel.getBounds().y + txLabel.getBounds().height + txLabel.getText().split("\n").length*HEIGHT_MAC_LABEL + HEIGHT_MAC_LABEL);
                        }
                    }
                }
            }
            String sender_mac;
            String num_recv;
            String sid;
            String[][] sender_list = new String[NetVisualize.MAX_NUM_NODES_DISPLAY][3];
            int sender_cnt = 0;
            for (int i = (strArr1.length - 1); i > 0 && (strArr1.length - 1 - i) < NetVisualize.FILE_BUF_LENGTH2; i--)  {
                String[] strArr2 = strArr1[i].split(" "); // used to split lines in strArr1
                if (strArr2.length>1) {
                    if (strArr2[1].trim().equals("Receiver")) {
                        if (strArr2[3].trim().equals(mac)) {
                            for (int j = i; j > 0 && (strArr1.length - 1 - j) < NetVisualize.FILE_BUF_LENGTH2; j--)  {
                                String[] strArr3 = strArr1[j].split(" ");
                                if (strArr3.length>1) {
                                    if (strArr3[1].trim().equals("SENDER")) {
                                        sender_mac = strArr3[3].trim();
                                        num_recv = strArr2[5].trim();
                                        sid = strArr2[9].trim();

                                        if (sender_cnt == 0) {
                                            sender_list[0][0] = sender_mac;
                                            sender_list[0][1] = num_recv;
                                            sender_list[0][2] = sid;
                                            sender_cnt = 1;
                                        }
                                        else {
                                            int found_flag = 0;
                                            for (int k=0; k<sender_cnt; k++) {
                                                if (sender_list[k][0].trim().equals(sender_mac.trim())) {
                                                    found_flag = 1;
                                                }
                                            }
                                            if (found_flag == 0) {
                                                sender_list[sender_cnt][0] = sender_mac;
                                                sender_list[sender_cnt][1] = num_recv;
                                                sender_list[sender_cnt][2] = sid;
                                                sender_cnt++;
                                            }
                                        }
                                        break;
                                    }
                                }
                            }
                        }
                    }
                }
            }

            String text;
            text = "<HTML>Received packets: \n";
            rxLabel.setText(text);
            for (int i=0; i<sender_cnt; i++) {
                text = rxLabel.getText();
                text = text + "<BR> " + sender_list[i][1] + " from " + sender_list[i][0] + " in session " + sender_list[i][2] + "\n";
                rxLabel.setText(text);
            }
            rxLabel.setBounds(LEFT_MAC_LABEL, txLabel.getBounds().y + txLabel.getBounds().height + VERT_GAP_IN_LABEL , WIDTH_MAC_LABEL, rxLabel.getText().split("\n").length*20 +20);
            this.setSize(FORM_WIDTH, rxLabel.getBounds().y + rxLabel.getBounds().height + 20);
        }
    }
       
    public void setLabelMac (String str) {
        macLabel.setText("Mac - " + str);
        mac = str;
    }
    
    public void setLabelDist (String str) {
        double connStrength;
        int connStr;
        
        connStrength = Double.parseDouble(str);
        connStrength = connStrength/255;
        connStrength = 1 - connStrength;
        connStr = (int)(connStrength * 10);
        distLabel.setText("Connection Strength: " + connStr + "/10 with Me");
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables
    private javax.swing.JLabel distLabel;
    private javax.swing.JLabel macLabel;
    private javax.swing.JLabel nameLabel;
    private javax.swing.JLabel ipLabel;
    private javax.swing.JLabel txLabel;
    private javax.swing.JLabel rxLabel;
}
